import $service from '../../../../service'
const app = getApp();
Page({
  data: {
    cameraType: 'front',
    isHead: true,
    headImg: '',
    tongueImg: '',
    facePictureBase64: '',
    tonguePictureBase64: '',
    isShow:true
  },
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  onShow() {
    this.setData({
      isHead: true
    })
  },
  // 切换摄像头
  cutCamera() {
    let cameraType = this.data.cameraType === 'front' ? 'back': 'front'
    this.setData({
      cameraType
    })
  },
  // 拍照
  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.getFileSystemManager().readFile({
          filePath: res.tempImagePath, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: e => { //成功的回调
            if (this.data.isHead) {
              this.setData({
                headImg: res.tempImagePath,
                facePictureBase64: e.data,
                isHead: false
              })
              wx.setNavigationBarTitle({ title: '舌苔采集'})
            } else {
              this.setData({
                tongueImg: res.tempImagePath,
                tonguePictureBase64: e.data
              })
              this.questionnaire();
            }
          }
        })
      }
    })
  },
  questionnaire() {
    let params = {
      "facePictureBase64": this.data.facePictureBase64,
      "tonguePictureBase64": this.data.tonguePictureBase64,
    }
    app.http.questionnaire(params).then(res => {
      $service.setStore('questionnaire', JSON.stringify(res.data))
      wx.redirectTo({
        url: '../question/index'
      })
    }).catch(err => {
      $service.showToast(err.msg || '生成失败, 请重新采集')
      setTimeout(() => {
        wx.setNavigationBarTitle({ title: '体质检测'})
        this.setData({
          isHead: true,
          headImg: '',
          facePictureBase64: '',
          tongueImg: '',
          tonguePictureBase64: ''
        })
      }, 1000)
    })
  },
  //隐藏弹框
  changeShow(){
    this.setData({
      isShow:false
    })
  }
})
